using UnityEngine;
using System.Collections;
using System;

public class DrawingSpotsOnTexture : MonoBehaviour
{
	
	
	
	
	public static bool CompareColor (Color32 lhs, Color32 rhs)
	{
		if ((lhs.r == rhs.r) && (lhs.g == rhs.g) && (lhs.b == rhs.b)) {
			return true;
		}
		return false;
		
	}
	
	public static bool CompareColorR (Color32 lhs, Color32 rhs)
	{
		if ((lhs.r == rhs.r)) {// && (lhs.g == rhs.g) && (lhs.b == rhs.b)) {
			return true;
		}
		return false;
		
	}
	
	
	public static void SymbolizeSpot (int Width, int Height, ArrayList SymbolPoints, Color32[] outputPixels, 
		                              int pointwidth/*only 1 3 now*/ , ArrayList ColorArr )
	{
		int lcount = 0;
		if (SymbolPoints.Count > 0)
			foreach (Vector2 vct2 in SymbolPoints) {		
				byte r = 0, g = 0, b = 0;
		
				r =(byte)(((Color)ColorArr[lcount]).r*255f);             //(((lcount + 2) % 3) * 80) % 255;
			    g =(byte)(((Color)ColorArr[lcount]).g*255f);                //(((lcount + 1) % 3) * 80) % 255;
			    b =(byte)(((Color)ColorArr[lcount]).b*255f);                   //(((lcount + 0) % 3) * 80) % 255;
			   
			  	WhiteWriteSquare (Width, Height, vct2.x,vct2.y, pointwidth, outputPixels, r, g, b);
				lcount++;
			}
	}
	
	
	
	
	
	
	
	
	public static  void WhiteWriteSquare (int Width, int Height, float tmpx, float tmpy, int pointwidth, Color32[] outputPixels, byte r, byte g, byte b)
	{
		if ((tmpx >= 0.0f)&&(tmpx<=1.0f)&&(tmpy>=0.0f)&&(tmpy <= 1.0f)) {
			int row = (int)(tmpy * ((float)(Height)));
			int col = (int)(tmpx * ((float)(Width)));		
	      		for (int i=0; i<pointwidth; i++) {
				for (int j=0; j<pointwidth; j++) {
					WhiteWritePixel (Width * (row + i - pointwidth / 2) + col + j - pointwidth / 2, outputPixels, r, g, b);				
				}
			}
		}	
	}
		

	public static  void WhiteWriteSquare (int Width, int Height, int tmpx, int tmpy, int pointwidth, Color32[] outputPixels, byte r, byte g, byte b)
	{

		int row = Mathf.Clamp(tmpy,0,Height-1);
		int col = Mathf.Clamp(tmpx ,0,Width-1);		
			for (int i=0; i<pointwidth; i++) {
				for (int j=0; j<pointwidth; j++) {
					WhiteWritePixel (Width * (row + i - pointwidth / 2) + col + j - pointwidth / 2, outputPixels, r, g, b);				
				}
			}
	}


	private static  void WhiteWritePixel (int count, Color32[] outputPixels, byte r, byte g, byte b)
	{
		if ((count > 0) && (count < outputPixels.Length - 1)) {
			outputPixels [count].r = r;
			outputPixels [count].g = g;
			outputPixels [count].b = b;
			outputPixels [count].a =255;
		}
		
	}
	
	public static float minf (float a, float b)
	{
		if (a < b) {
			return a;
		}
		return b;		
	}
	
	
	public static  void DrawPixelsSquareOnByteArray (int Width, int Height, int tmpx, int tmpy, int pointwidth, byte[]  outputPixels, byte r, byte g, byte b,byte a)
	{
		
		int row = Mathf.Clamp(tmpy,0,Height-1);
		int col = Mathf.Clamp(tmpx ,0,Width-1);		
		for (int i=0; i<pointwidth; i++) {
			for (int j=0; j<pointwidth; j++) {

				int pixPosLen=Width * (row + i - pointwidth / 2) + col + j - pointwidth / 2;
				int arrlen=Width*Height-1;

				if(pixPosLen<arrlen)
				{
				  DrawPixelOnByteArray (Width * (row + i - pointwidth / 2) + col + j - pointwidth / 2, outputPixels, r, g, b,a);				
				}
			}
		}
	}



	
	private static  void DrawPixelOnByteArray (int count, byte[] outputPixels, byte r, byte g, byte b,byte a)
	{
		if ((count > 0) && (count < outputPixels.Length - 1)) 
		{
			outputPixels[count * 4] = (byte)(r );
			outputPixels[count * 4 + 1] = (byte)(g);
			outputPixels[count * 4 + 2] = (byte)(b);
			outputPixels[count * 4 + 3] = (byte)(a);	
		}		
	}
	
		
	
	
	
	// Use this for initialization
	void Start ()
	{
	
	}
	
	// Update is called once per frame
	void Update ()
	{
	
	}
}
